package main

import (
	"flag"
	"fmt"
	"gitee.com/wolferhua/gostudy/algorithm/bitmap/bitmap"
	"gitee.com/wolferhua/gostudy/algorithm/utils"
	"github.com/imroc/biu"
	//"math"
	"os"
	"sync"
)

func main() {



	flag.Parse()
	if flag.Arg(0) == "gen" {
		//GenNums(1024, int64(math.MaxInt32))
		fmt.Println(utils.GetRandSlices(10))
		return
	}
	i := byte(255)
	fmt.Println(biu.ByteToBinaryString(i))
	fmt.Println(biu.ByteToBinaryString(i >> 7))
	fmt.Println(biu.ByteToBinaryString(byte(1) | byte(2)))
	fmt.Println(biu.ByteToBinaryString(byte(1) | byte(3)))
	fmt.Println(biu.ByteToBinaryString(byte(1) | byte(4)))

	bm := &bitmap.Bitmap{}
	bm.SetBit(100, true)
	bm.SetBit(12, true)
	bm.SetBit(1, true)
	bm.SetBit(12, true)
	fmt.Println("bit:100：",bm.Get(100))
	fmt.Println("bit:12：",bm.Get(12))

	bm.SetBit(12, false)
	fmt.Println("bit:12：",bm.Get(12))
}
func GenNums(pool int, max int64) {
	//max := int64(18) //int64(math.MaxInt32)
	rangeSize := max / int64(pool)
	wg := &sync.WaitGroup{}

	nums := utils.GetRandSlices(int(rangeSize), 10)
	for i := 0; i < pool; i++ {
		wg.Add(1)
		start := int64(i) * rangeSize
		end := start + rangeSize - 1
		if end > max {
			end = max
		}
		//fmt.Println(start)
		//fmt.Println(end)
		go RangeGen(i, start, end, nums, wg)

	}
	fmt.Println(rangeSize)
	wg.Wait()
}

func RangeGen(i int, start, end int64, nums []int, group *sync.WaitGroup) {
	defer func() {
		group.Done()
	}()
	f, err := os.OpenFile(fmt.Sprintf("data/%05d.txt", i), os.O_CREATE|os.O_WRONLY, 0666)
	if err != nil {
		return
	}
	defer f.Close()

	//fmt.Print("start:", start)
	//fmt.Print("\tend:", end)
	//fmt.Println("\trangeSize:", rangeSize)
	for _, num := range nums {
		f.WriteString(fmt.Sprintf("%d\n", start+int64(num)))
	}

}
